using System;
using System.Collections.Generic;
using System.Text;
using Ent.Web.Helper;
using System.Configuration;
using System.Web;
namespace Ent.Web.Mail
{
    public struct MailContent
    {
        public string Subject;
        public string Body;
        public MailContent(string subject, string body)
        {
            Subject = subject;
            Body = body;
        }
    }
    public class Collection
    {
        private static string _admin = "";
        private static string _header = "";
        private static string _signature = "";
        private static string _footer = "";
        private static Dictionary<string, MailContent> _mails = null;
        static Dictionary<string, MailContent> Mails
        {
            get
            {
                if (_mails == null)
                    Ent.Web.Mail.Collection.Init(ConfigurationManager.AppSettings.Get("AutoMail"));
                return _mails;
            }
        }
        public static string Admin
        {
            get { return _admin; }
        }
        public static void Init(string path)
        {
            path = HttpRuntime.AppDomainAppPath + "/" + path;
            if (_mails == null)
            {
                _mails = new Dictionary<string, MailContent>();
                XMLHelper _xml = new XMLHelper(path);
                _admin = _xml.GetInnerText("//Voice/Admin");
                _header = _xml.GetInnerText("//Voice/Header");
                _signature = _xml.GetInnerText("//Voice/Signature");
                _footer = _xml.GetInnerText("//Voice/Footer");
                string root = "//Voice/AutoMail";
                string[] names = _xml.GetAttributesOfAllNodes(root, "name");
                foreach (string name in names)
                {
                    string xpath = root + "[@name='" + name + "']";
                    _mails.Add(name, new MailContent(_xml.GetInnerText(xpath + "/Subject"), _header + _xml.GetInnerText(xpath + "/Body") + _signature + _footer));
                }
            }
        }
        public static MailContent GetMailContent(string contentName)
        {
            if (Mails.ContainsKey(contentName))
                return Mails[contentName];
            else return new MailContent();
        }
    }
}
